package server;

import gui.basicchart.BasicChart;
import gui.basicchart.BasicChartPanel;
import gui.body.BodyFrame;

import javax.swing.SwingUtilities;

import org.omg.CORBA.ORB;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import KiBeam.DataImpl;

public class Server {
	
	public static int maxUsers = 16;
	
	public static void main(String[] args) {
		
		final BasicChartPanel GUI = new BasicChartPanel(maxUsers);
		final BodyFrame body = new BodyFrame(maxUsers);
		
		try {
			ORB orb = ORB.init(args, null);
			POA rootpoa = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();
			DataImpl data = new DataImpl();
			
			// add listeners to display data
			data.addListener(GUI.getChartDataListener());
			data.addListener(body.getBodyDataListener());
			
			body.setLocation((int)GUI.getSize().getWidth(), 0);
			org.omg.CORBA.Object objref = rootpoa.servant_to_reference(data);
			String ref = orb.object_to_string(objref);
			java.io.FileOutputStream file = new java.io.FileOutputStream(
					"ObjectID");
			java.io.PrintStream output = new java.io.PrintStream(file);
			output.println(ref);
			output.close();
			
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					GUI.setVisible(true);
					body.setVisible(true);
				}
			});
			
			
			System.out.println("Ready !");
			System.out.println("./Client "+ ref);
			GUI.setState(ref);
			orb.run();					



		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
